// page/index/index.js

const app=getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: '', //搜索的内容
    count:1,
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,      //是否自动切换
    interval: 3000,       //自动切换时间间隔
    duration: 1000,       //滑动动画时长
    inputShowed: false,
    inputVal: "",
    banners:[],
    news:[],
    baseURL:wx.baseURL,
    newsIndex:0,
    newsTimer:0,
    newIndes:'transform:translate(0rpx,0rpx)',
    pageNum:1,
    total:0,
    loading:'加载中...',
    order:[],
    shop_recommmend:[],
  },

   getBanners(){//获取轮播图
    return new Promise((resolve,reject)=>{
      wx.get('/index/banners').then(res=>{
        this.setData({
          banners:res
        })
        resolve()
      })
    })
  
    },
    swiperClick(){//轮播图跳转
      let path=this.data.banners.path
      wx.switchTab({
        url:'/page/classify/classify',
      })
    },
 

  tapNameBig(even){
    console.log('tapNameBig',even)
    let count=this.data.count
    count++
    this.setData({
      count:count
    })
    this.data.count=count
  },
  // tapName(event){
  //   console.log('点击成功',event)
  //   let url=event.currentTarget.dataset.path
  //   wx.showModal({
  //     cancelColor: 'cancelColor',
  //     title:'提示',
  //     content:'是否跳转到'+url+'界面',
  //     success(e){
  //       console.log(url)
  //       wx.switchTab({
  //         url,
  //       })
  //     }
  //   })
  // },

 
  
  getNews(){
    return new Promise((resolve,reject)=>{
      wx.get('/index/news').then(res=>{
        this.setData({
          news:res
        })
        this.changeNewsIndex()//触发轮播
        resolve()
      })
    })
   
  },

  getOrder(){
    return new Promise((resolve,reject)=>{
      wx.get('/index/categorys').then(res=>{
        this.setData({
          order:res
        })
        resolve()
      })
    })
   
  },

  changeNewsIndex(){
    if(this.data.newsTimer){
      clearInterval(this.data.newsTimer)
      this.data.newsTimer = 0
    }
    this.data.newsTimer = setInterval(() =>{
      let newsIndex = this.data.newsIndex
      newsIndex ++
      // console.log(newsIndex);
      this.setData({
        newsIndex,
        newIndes:'transform:translate(0rpx,-' + newsIndex * 80 + 'rpx);transition:transform 0.3s ease;'
      })
      if(this.data.newsIndex==2){
        setTimeout(()=>{
          this.setData({
            newsIndex:0,
            newIndes:'transform:translate(0rpx,0rpx);'
          })
        },500)
      }
    },3000)
  },

  //刷新
  onPullDownRefresh(){
   this.data.pageNum = 1
   this.data.shop_recommmend=[]
   this.getList()
  },

  //页面上拉触底事件的处理函数
  onReachBottom(){
    if(this.data.total > this.data.shop_recommmend.length){
      this.data.pageNum ++
      this.getRecommend()
      // console.log(this.data.pageNum)
    }  
  },

  getRecommend(){
    return new Promise((resolve,reject)=>{
      this.setData({loading:'加载中...'})
      wx.get('/index/recommends',{
        pageNum:this.data.pageNum
      }).then(res=>{
        console.log(res)
        this.setData({
          loading:'已经到底了哦~',
          shop_recommmend:this.data.shop_recommmend.concat(res.rows),
          total:res.total
        })
        resolve()
      })
    })
  },

  
  getList(){
    Promise.all([this.getNews(),this.getBanners(),this.getOrder(),this.getRecommend()]).then(()=>{
      console.log('所有数据加载完成')
    }).catch(e=>{
      console.log(e)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(){
   this.getList()
  //  this.getBanners()//获取轮播图
  //  this.changeNewsIndex()//新闻轮播
  //  this.getOrder()
  //  this.getRecommend()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(){
    // console.log('I an aready')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    // console.log('I am page onshow')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})